package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"mxshop_srvs/user_srv/proto"
	"strconv"
)

var conn *grpc.ClientConn
var client proto.UserClient

func Init() {
	var err error
	conn, err = grpc.Dial("127.0.0.1:8888", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		panic(err)
	}
	client = proto.NewUserClient(conn)
}

func TestCreateUser() {

	for i := 0; i < 10; i++ {
		user, err := client.CreateUser(context.Background(), &proto.CreateUserInfo{
			Mobile:   fmt.Sprintf("1871112234%d", i),
			Password: "admin123",
			NickName: fmt.Sprintf("bobby%d", i),
		})
		if err != nil {
			return
		}
		fmt.Println(user.Id)
	}
}

func main() {

	Init()
	TestCreateUser()
	TestGetUserList()
	TestGetUserById()
	TestGetUserByMobile()
	TestUpdateUser()
	conn.Close()
}

func TestUpdateUser() {

	_, err := client.UpdateUser(context.Background(), &proto.UpdateUserInfo{
		Id:       2,
		NickName: "哈哈",
		BirthDay: 1684227393,
	})
	if err != nil {
		panic(err)
	}
}

func TestGetUserByMobile() {

	userInfo, err := client.GetUserByMobile(context.Background(), &proto.MobileRequest{Mobile: strconv.Itoa(18711122341)})
	if err != nil {
		panic(err)
	}
	fmt.Println(userInfo)
}

func TestGetUserById() {

	userInfo, err := client.GetUserById(context.Background(), &proto.IdRequest{Id: 1})
	if err != nil {
		panic(err)
	}
	fmt.Println(userInfo)
}

func TestGetUserList() {

	rep, err := client.GetUserList(context.Background(), &proto.PageInfo{
		Pn:    1,
		PSize: 5,
	})
	if err != nil {
		panic(err)
	}
	for _, userInfo := range rep.Data {
		fmt.Println(userInfo)
		checkRsp, err := client.CheckPassword(context.Background(), &proto.PasswordCheckInfo{
			Password:          "admin123",
			EncryptedPassword: userInfo.Password,
		})
		if err != nil {
			panic(err)
		}
		fmt.Println(checkRsp.Success)
	}
}
